var config = require('../../config');

Page({
  /**
     * 页面的初始数据
     */
  data: {
    topImgs: [
    ],
    hostName: config.service.host,
    objHostName: config.service.objHostName,
    newsList: {},
    // 请求数据索引值
    ye: 1,
    pageSize: 5,
    hideTopLabel: true,
    hideBottomLabel: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      ye: 1
    });
    // 请求资讯列表
    this.ZS_REQUEST_infoList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    if (that.data.topImgs.length == 0) {
      that.getTopImgs()
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      ye: 1,
      hideTopLabel: false
    });
    console.log('date.ye(页):' + this.data.ye);
    // 请求资讯列表
    this.ZS_REQUEST_infoList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    that.setData({
      ye: ++that.data.ye,
      hideTopLabel: true
    });
    console.log('date.ye(页):' + that.data.ye);

    this.ZS_REQUEST_infoList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  getTopImgs: function () {
    wx.showLoading({
      title: '获取顶部图片',
    })
    var that = this;
    wx.request({
      url: `${config.service.topImgUrl}` + '?type=1',
      method: 'GET',
      timeout: 2000,
      data: {},
      success: function (e) {
        wx.hideLoading()
        if (null == e.data.code) {
          wx.showToast({
            title: '获取顶部图片失败',
          })
          return
        }

        let code = e.data.code
        if (1 == code) {
          wx.showToast({
            title: '' + e.data.msg,
          })
          return
        }
        let res = e.data.res
        // console.log("顶部res:"+res)

        var imgs = res.imgs.split(',')

        that.setData({
          topImgs: imgs
        })
      },
      fail: f => {
        wx.hideLoading()
        console.log('f:' + f)
        wx.showToast({
          title: '失败:获取地区',
        })
      }

    })
  },

  ZS_REQUEST_infoList: function () {
    wx.stopPullDownRefresh()
    wx.showNavigationBarLoading();
    var that = this;
    // 请求资讯列表
    wx.request({
      url: `${config.service.newsUrl}`,
      data: {
        ye: that.data.ye,
        row: that.data.pageSize
      },
      success: function (e) {
        wx.hideNavigationBarLoading();
        that.setData({
          hideTopLabel: true
        });
        var code = parseInt(e.data.code);
        // console.log("资讯列表接口:" + JSON.stringify(e));
        if (code == 0) {
          var res = e.data.res;

          if (res.length) {
            for(var i=0;i<res.length;i++){
              var news = res[i]
              news.date = news.date.substr(0,10)
            }
            that.setData({
              newsList: that.data.newsList.length && that.data.ye != 1 ? that.data.newsList.concat(res) : res
            });

            if (res.length < that.data.pageSize) {
              that.setData({
                hideBottomLabel: false
              });
            }
          }
          else {
            that.setData({
              hideBottomLabel: false
            });
          }
        }
        else {
          var msg = e.data.msg;
          if (msg == null) {
            msg = JSON.stringify(e);
          }
          console.log(msg);
          wx.showToast({
            title: '异常:' + msg + "[" + code + "]",
          })
        }
      },
      fail: function (failed) {
        console.log(JSON.stringify(failed));
        wx.hideNavigationBarLoading();
        that.setData({
          hideTopLabel: true
        });
        wx.showModal({
          title: '访问失败',
          content: JSON.stringify(failed),
        })
      },
    });
  },
})